//
//  YDEnterVerifyCodeAlert.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/2/7.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDEnterVerifyCodeAlert.h"
#import "OttoKeyboardView.h"


@interface YDEnterVerifyCodeAlert ()<TextFieldClickReturnDelegate,UITextFieldDelegate>


@property (nonatomic, copy) InputFinishBlock finishBlock;
@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong)UILabel *bankLbl;  //银行
@property (nonatomic, strong)UILabel *moneyLbl; //钱
@property (nonatomic, strong)UILabel *phoneLbl; //手机号显示
@property (nonatomic, strong)UILabel *line;

@property (nonatomic, strong) OttoTextField *textfield;//输入验证码

@property (nonatomic, strong)UIButton *huoquBtn; //获取验证码按钮

@property (nonatomic, strong)UIButton *comfirmBtn; //确认支付

@property (nonatomic, strong)NSTimer *timber;
@property (nonatomic, assign)NSInteger second;


@end

@implementation YDEnterVerifyCodeAlert


- (instancetype)initWithFrame:(CGRect)frame inputFinish:(InputFinishBlock)block
{
    self.finishBlock = block;
    
    if (self = [super initWithFrame:frame]) {
        
        
        UIView *maskView = [[UIView alloc] initWithFrame:self.bounds];
        maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        
        [maskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClose:)]];
        
        [self addSubview:maskView];
        [self mainView];
        [self bankLbl];
        [self moneyLbl];
        [self phoneLbl];
        [self line];
        [self textfield];
        
        [self comfirmBtn];
        [self huoquBtn];
        
    }
    
    return self;
    
}
- (void)setModel:(YDBankCardModel *)model
{
    _model = model;
    self.bankLbl.text = [NSString stringWithFormat:@"银行卡号：%@",model.fourStarFormatterBankCard];
    self.moneyLbl.text = [NSString stringWithFormat:@"充值金额：%@元",self.money];
    self.phoneLbl.text = [NSString stringWithFormat:@"短信验证码已发送至%@",[NSString phoneNumberasterisk:model.phone]];
    
}

- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.second = 60;
    [self openTimber];
}




- (void)tapClose:(UITapGestureRecognizer *)tap
{
    [self hide];
}



- (void)hide
{
    [self closeTimber];
    [self removeFromSuperview];
}


/**
 点击重新获取
 */
- (void)codeBtnDidSelected:(UIButton *)sender
{
    self.second = 60;
    [self openTimber];
    if (self.finishBlock) {
        self.finishBlock(sender.titleLabel.text);
    }
}


/**
 点击确认
 */
- (void)confirmDidSelected:(UIButton *)sender
{
    [self hide];
    
    if (self.finishBlock) {
        self.finishBlock(self.textfield.text);
    }
}

- (void)textFieldClickReturn:(OttoTextField *)textField
{
    if (textField == self.textfield) {
        [textField resignFirstResponder];
    }
}


- (void)openTimber
{
    if (self.timber == nil) {
        self.timber = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeBtnTitle) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timber forMode:NSRunLoopCommonModes];
    }
}

- (void)changeBtnTitle
{
  
    self.second -- ;
    if (self.second == 0) {
        [self closeTimber];
        [self.huoquBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        self.huoquBtn.enabled = YES;
    }else
    {
       [self.huoquBtn setTitle:[NSString stringWithFormat:@"%lds",self.second] forState:UIControlStateNormal];
        self.huoquBtn.enabled = NO;
    }
    
    
}

- (void)closeTimber
{
    if (self.timber) {
        [self.timber invalidate];
        self.timber = nil;
    }
}
- (UIView *)mainView
{
    if (!_mainView) {
        
        CGFloat leftGap = 22;
        _mainView = [[UIView alloc] init];
        _mainView.backgroundColor = [UIColor whiteColor];
        _mainView.layer.cornerRadius = 8;
        [self addSubview:_mainView];
        
        [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(rate375(leftGap)));
            make.right.equalTo(@(rate375(-leftGap)));
           
            
            if (isPhoneX) {
                make.top.equalTo(self).offset(rateX812(156));
                make.height.equalTo(@(rateX812(273)));
            }else
            {
                make.top.equalTo(self).offset(rate750(156));
                make.height.equalTo(@(rate750(273)));
            }
            
        }];
        
        
       
       
        
        
        UILabel *titleLbl = [[UILabel alloc] init];
        titleLbl.text = @"请输入短信验证码";
        [_mainView addSubview:titleLbl];
        titleLbl.textAlignment = NSTextAlignmentCenter;
        titleLbl.font = [UIFont systemFontOfSize:font20ScreenWRate * LKScreenW];
        titleLbl.textColor = [UIColor colorWithHexString:dark];
        [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(isPhoneX ? @(rateX812(11)) : @(rate750(11)));
            make.centerX.equalTo(_mainView);
            
        }];
        
        UIImageView *closeIcon = [[UIImageView alloc] init];
        closeIcon.userInteractionEnabled = YES;
        closeIcon.image = [UIImage imageNamed:@"zhifu-quxiao"];
        [_mainView addSubview:closeIcon];
        [closeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@26);
            make.centerY.equalTo(titleLbl.mas_centerY);
        }];
        
        UIView *tapView = [UIView new];
        tapView.userInteractionEnabled = YES;
        
        [_mainView addSubview:tapView];
        [tapView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(_mainView);
            make.height.width.equalTo(@40);
        }];
        
        [tapView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClose:)]];
        
        
        
        CGFloat lineLeftGap = 21;
        UILabel *line = [[UILabel alloc] init];
        [_mainView addSubview:line];
        line.backgroundColor = NumberColor(239, 239, 239);
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@50);
            make.left.equalTo(@(lineLeftGap));
            make.right.equalTo(@(-lineLeftGap));
            make.height.equalTo(@1);
        }];
    }
    return _mainView;
}

- (UILabel *)bankLbl
{
    if (!_bankLbl) {
        _bankLbl = [UILabel new];
        _bankLbl.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        _bankLbl.textColor = NumberColor(30, 36, 46);
        [self.mainView addSubview:_bankLbl];
        [_bankLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mainView).offset(isPhoneX? rateX812(64) : rate750(64));
            make.left.equalTo(self.mainView.mas_left).offset(rate375(44));
            make.right.equalTo(self.mainView.mas_right).offset(rate375(-44));
        }];
        _bankLbl.text = @"银行卡号：6221 **** **** 223";
    }
    return _bankLbl;
}
- (UILabel *)moneyLbl
{
    if (!_moneyLbl) {
        _moneyLbl = [UILabel new];
        _moneyLbl.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        _moneyLbl.textColor = NumberColor(30, 36, 46);
        [self.mainView addSubview:_moneyLbl];
        [_moneyLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bankLbl.mas_bottom).offset(isPhoneX ? rateX812(6) : rate750(6));
            make.left.equalTo(_bankLbl.mas_left);
            make.right.equalTo(_bankLbl.mas_right);
        }];
        _moneyLbl.text = @"充值金额：1000元";
    }
    return _moneyLbl;
}

-(UILabel *)phoneLbl
{
    if (!_phoneLbl) {
        _phoneLbl = [UILabel new];
        _phoneLbl.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        _phoneLbl.textColor = NumberColor(30, 36, 46);
        [self.mainView addSubview:_phoneLbl];
        [_phoneLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.moneyLbl.mas_bottom).offset(isPhoneX ? rateX812(6) : rate750(6));
            make.left.equalTo(_bankLbl.mas_left);
            make.right.equalTo(_bankLbl.mas_right);
        }];
        _phoneLbl.text = @"短信验证码已发送至186****1012";
    }
    return _phoneLbl;
}

- (OttoTextField *)textfield
{
    if (!_textfield) {
        
        CGFloat textFieldY = 24;
        CGFloat textFieldH = 20;
        
        _textfield = [[OttoTextField alloc] init];
        [_textfield becomeFirstResponder];
        _textfield.delegate = self;
        _textfield.placeholder = @"请输入验证码";
        _textfield.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
    
        _textfield.textAlignment = NSTextAlignmentCenter;
        _textfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _textfield.textFieldClickReturnDelegate = self;
        
        [_textfield setKeyboardType:KeyboardTypeNumber];
        [_textfield setNumberKeyboardType:NumberKeyboardTypeNumber];
        
        [_mainView addSubview:_textfield];
        [_textfield mas_makeConstraints:^(MASConstraintMaker *make) {

            make.width.equalTo(@(rate375(90)));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(textFieldH)));
                make.top.equalTo(self.phoneLbl.mas_bottom).offset(rateX812(textFieldY));
            }else{
                make.height.equalTo(@(rate750(textFieldH)));
                make.top.equalTo(self.phoneLbl.mas_bottom).offset(rate750(textFieldY));
            }
            
            make.centerX.equalTo(_line.mas_centerX);
            
        }];
        
    }
    return _textfield;
}

-(UILabel *)line
{
    if (!_line) {
        _line = [UILabel new];
        _line.backgroundColor = NumberColor(151, 151, 151);
        [_mainView addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@1);
            make.top.mas_equalTo(_phoneLbl.mas_bottom).offset(isPhoneX ? rateX812(54) : rate750(54));
            make.width.equalTo(@(rate375(162)));
            make.left.equalTo(_bankLbl.mas_left);
            
        }];
        
    }
    return _line;
}

-(UIButton *)huoquBtn
{
    if (!_huoquBtn) {
        _huoquBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_mainView addSubview:_huoquBtn];
        _huoquBtn.backgroundColor = NumberColor(216, 216, 216);
        [_huoquBtn setTintColor:[UIColor whiteColor]];
        _huoquBtn.layer.cornerRadius = 4;
        _huoquBtn.layer.masksToBounds = YES;
        [_huoquBtn setTitle:@"60s" forState:UIControlStateNormal];
        [_huoquBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_bankLbl.mas_right);
            make.centerY.equalTo(_textfield.mas_centerY);
            make.width.equalTo(@(rate375(80)));
            make.height.equalTo(isPhoneX ? @(rateX812(28)) : @(rate750(28)));
            
        }];
        _huoquBtn.titleLabel.font = [UIFont systemFontOfSize:font12ScreenWRate *LKScreenW];
        [_huoquBtn addTarget:self action:@selector(codeBtnDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _huoquBtn;
}

- (UIButton *)comfirmBtn
{
    if (!_comfirmBtn) {
        _comfirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_mainView addSubview:_comfirmBtn];
        _comfirmBtn.backgroundColor = [UIColor colorWithHexString:Orange];
        [_comfirmBtn setTintColor:[UIColor whiteColor]];
        [_comfirmBtn setTitle:@"确认支付" forState:UIControlStateNormal];
        _comfirmBtn.layer.cornerRadius = 4;
        _comfirmBtn.layer.masksToBounds = YES;
        _comfirmBtn.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate *LKScreenW];
        [_comfirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_bankLbl.mas_left);
            make.right.equalTo(_bankLbl.mas_right);
          
            
            
            if (isPhoneX) {
                make.top.equalTo(_line.mas_bottom).offset(rateX812(20));
                make.height.equalTo(@(rateX812(40)));
            }else
            {
                make.top.equalTo(_line.mas_bottom).offset(rate750(20));
                make.height.equalTo(@(rate750(40)));
            }
            make.centerX.equalTo(_mainView.mas_centerX);
        }];
        [_comfirmBtn addTarget:self action:@selector(confirmDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _comfirmBtn;
}
@end
